package com.todo.member.infrastructure.repository.impl;

import com.todo.member.common.enums.LevelRewardRecordStatusEnum;
import com.todo.member.infrastructure.entity.po.MemberLevelRewardRecordPO;
import com.todo.member.infrastructure.mapper.MemberLevelRewardRecordMapper;
import com.todo.member.infrastructure.repository.MemberLevelRewardRecordRepository;
import lombok.extern.slf4j.Slf4j;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/2/11 11:37
 * @description
 */
@Slf4j
@Repository
public class MemberLevelRewardRecordRepositoryImpl implements MemberLevelRewardRecordRepository {

    @Resource
    private MemberLevelRewardRecordMapper memberLevelRewardRecordMapper;

    @Override
    public Boolean insertMemberLevelRewardRecordPO(MemberLevelRewardRecordPO memberLevelRewardRecordPO) {
        if (memberLevelRewardRecordPO == null) {
            return false;
        }
        if (memberLevelRewardRecordPO.getUserId() == null || memberLevelRewardRecordPO.getLevelCode() == null || memberLevelRewardRecordPO.getRewardCode() == null) {
            return false;
        }
        memberLevelRewardRecordPO.setStatus(LevelRewardRecordStatusEnum.WAIT_DISTRIBUTE.getType());
        return memberLevelRewardRecordMapper.insertMemberLevelRewardRecordPO(memberLevelRewardRecordPO) > 0;
    }

    @Override
    public Boolean updateMemberLevelRewardRecordPOById(MemberLevelRewardRecordPO memberLevelRewardRecordPO) {
        if (memberLevelRewardRecordPO == null || memberLevelRewardRecordPO.getId() == null) {
            return false;
        }
        return memberLevelRewardRecordMapper.updateMemberLevelRewardRecordPOById(memberLevelRewardRecordPO) > 0;
    }

    @Override
    public Boolean updateMemberLevelRewardRecordStatusById(Long id, LevelRewardRecordStatusEnum oldStatus, LevelRewardRecordStatusEnum newStatus) {
        if (id == null || id < 0 || oldStatus == null || newStatus == null) {
            return false;
        }
        return memberLevelRewardRecordMapper.updateMemberLevelRewardRecordStatusById(id, oldStatus.getType(), newStatus.getType());
    }

    @Override
    public List<MemberLevelRewardRecordPO> queryMemberLevelRewardRecordPOByUser(Long userId, String levelCode) {
        if (userId == null || userId < 0) {
            return Lists.newArrayList();
        }
        return memberLevelRewardRecordMapper.queryMemberLevelRewardRecordPOByUser(userId, levelCode);
    }
}
